package com.sc.servlet.admin.field;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sc.pojo.Field;
import com.sc.services.FieldServices;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * @description: 添加场地
 * @author: sc
 * @create: 2023-01-03 13:59
 **/
@WebServlet("/addField")
public class addFieldServlet extends HttpServlet {

    private FieldServices fieldServices = new FieldServices();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        String readLine = reader.readLine();
        JSONObject jsonObject = JSONObject.parseObject(readLine);
        String res=null;


        Field addfield = new Field();

        System.out.println(jsonObject.getString("fieldpicture"));
        addfield.setFieldincharge(jsonObject.getString("fieldincharge"));
        addfield.setFieldinformation(jsonObject.getString("fieldinformation"));
        addfield.setFieldpicture(jsonObject.getString("fieldpicture"));
        addfield.setFieldlocation(jsonObject.getString("fieldlocation"));
        addfield.setFieldstate(jsonObject.getString("fieldstate"));
        addfield.setFieldname(jsonObject.getString("fieldname"));
        System.out.println(addfield);
        if(addfield.getFieldinformation()==""||addfield.getFieldname()==""||addfield.getFieldlocation()==null
        ||addfield.getFieldpicture()==null)
        {
            res = "not_all";
        }else {
            try {
                fieldServices.addField(addfield);
                res = "add_suc";
            }catch (Exception e){
                res = "wrong_adminname";
            }
        }
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(res);



    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}